package com.sp.scmdb.action;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.sp.scmdb.bean.RoleBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.User;
import com.sp.scmdb.service.RoleService;
import com.sp.scmdb.service.UserService;


@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<UserBean> {

	/**
	 * 用户Action
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(UserAction.class);
	private UserService userService;
	private RoleService roleService;

	/**
	 * @param roleService
	 * the roleService to set
	 */
	@Autowired
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @param userService
	 * the userService to set
	 */
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		
		UserBean userBean = null;
		if (StringUtils.isNotEmpty(pageName)) {
			userBean = (UserBean)session.get("userBean");
		}else{
			entity.setPageNo(1);
		}
		entity = userService.queryAll(entity, userBean);
		List<RoleBean> roleList = roleService.findAll();
		session.put("roleList", roleList);
		session.put("userBean", entity);

		// 页面”添加“，”修改“，”删除“按钮的权限设置
		// 调用BaseAction的button(String urlAdd, String urlEdit, String urlDel)方法
		this.button("Super.Popup(5,'添加','/SCMDB/userAction!newUser.action','',300,190)", 
				"Super.editData('/SCMDB/userAction!findUser.action?id=',350,210)", 
				"Super.delData('/SCMDB/userAction!delUser.action?id=')");
		
		return SUCCESS;
	}
	public String newUser(){
		return "newSucc";
	}
	public String addUser() {
		try {
			UserBean oldUser = (UserBean)session.get("user");
			getEntity().setOldUserName(oldUser.getUserName());
			String flg = userService.newUser(getEntity());
			messageSession(flg, "用户");
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("add fail|reason:"+e.getMessage());
		}
		return "commitSucc";
	}

	public String findUser() {
		try {
			
			String userId = request.getParameter("id");
			List<User> userList=userService.findUser(userId);
			List<RoleBean> roleList = roleService.findAll();
			request.setAttribute("roleList", roleList);
			request.setAttribute("userList", userList);
		} catch (Exception e) {
			log.error("add fail|reason: "+e.getMessage());
		}
		return "editSucc";
	}

	public String editUser() {
		try {
			UserBean oldUser = (UserBean)session.get("user");
			getEntity().setOldUserName(oldUser.getUserName());
			//String userId = request.getParameter("userId");
			String flg = userService.editUser(entity);
			if("3".equals(flg)){
				messageSession("0", "用户名重复！");	
			}else{
				messageSession(flg, "用户");
			}
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
		} catch (Exception e) {
			log.error("edit fail|reason:" +e.getMessage());
		}
		return "commitSucc";
	}

	public String delUser() {
		try {
			UserBean oldUser = (UserBean)session.get("user");
			getEntity().setOldUserName(oldUser.getUserName());
			String flg = null;
			String userId = request.getParameter("id");
			if (StringUtils.isNotEmpty(userId)) {
				String[] arr = userId.split(",");
				flg = userService.delUser(arr,entity);
			}
			if("5".equals(flg)){
				messageSession("0","系统保留用户，请不要删除！");
			}else{
				messageSession(flg, "用户");
			}
			
			if (log.isInfoEnabled()) {
				log.info("delete suncce");
			}
		} catch (Exception e) {
			log.error("delete fail|reason:"+e.getMessage());
		}
		return "commitSucc";
	}
	
	public String jumpUser() {

		return "newSucc";
	}
}
